<?php

class Ext_View_Helper_AjaxLoader extends Zend_View_Helper_HtmlElement
{
    protected $_defaultArgs = array(
        'autoOpen'      => false,
        'draggable'     => false,
        'modal'         => true,
        'resizable'     => false,
        'closeOnEscape' => true,
        'bgiframe'      => false,
        'minHeight'     => 50,
        'width'         => 200
    );

    public function ajaxLoader($id = 'ajax-loader', $args = array())
    {
        $script = "
            $(document).ajaxStart(function() {
                $('#{$id}').dialog('open');
                $('.ui-widget-overlay').css('opacity', 0.1);
            });

            $(document).ajaxComplete(function() {
                $('#{$id}').dialog('close');
            });
        ";

        $args = array_merge($this->_defaultArgs, $args);

        $args['dialogClass'] = $id;

        $result = $this->view->dialogContainer(
            $id,
            '<p>' . $this->view->translate('loading') . '</p>',
            $args
        );

        $this->view->JQuery()->addOnload($script);

        return $result;
    }
}